package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.HouseImage;
import com.atguigu.result.Result;
import com.atguigu.service.HouseImageService;
import com.atguigu.util.QiniuUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @ClassName HouseImageController
 * @Description 此类描述:
 * @Author dangchen
 * @DateTime 2022-08-25 15:31
 * @Version 1.0
 */
@RequestMapping("/houseImage")
@Controller
public class HouseImageController extends BaseController {
    @Reference
    private HouseImageService houseImageService;

    // /houseImage/uploadShow/[[${house.id}]]/1
    // /houseImage/uploadShow/[[${house.id}]]/2
    @RequestMapping("uploadShow/{houseId}/{type}")
    public String uploadShow(Model model, @PathVariable Long houseId,@PathVariable Integer type){
        model.addAttribute("houseId",houseId);
        model.addAttribute("type",type);
        return "house/upload";
    }

    ///houseImage/upload/[[${houseId}]]/[[${type}]]
    @RequestMapping("upload/{houseId}/{type}")
    @ResponseBody
    public Result upload(@PathVariable Long houseId, @PathVariable Integer type, @RequestParam(value = "file") MultipartFile[] files) throws IOException {
        if (files != null && files.length > 0){
            for (MultipartFile file : files){
                String originalFilename = file.getOriginalFilename();
                String fileName = UUID.randomUUID().toString();
                fileName += originalFilename.substring(originalFilename.lastIndexOf("."));
                System.out.println("HouseImageController fileName="+fileName);
                //上传图片
                String url  = "http://rh7a83nvu.hb-bkt.clouddn.com/" + fileName;
                QiniuUtils.upload2Qiniu(file.getBytes(),fileName);

                HouseImage houseImage = new HouseImage();
                houseImage.setHouseId(houseId);
                houseImage.setType(type);
                houseImage.setImageName(fileName);
                houseImage.setImageUrl(url);

                houseImageService.insert(houseImage);
            }
        }
        return Result.ok();
    }

    // /houseImage/delete/[[${house.id}]]/'+id
    @RequestMapping("delete/{houseId}/{id}")
    public String delete(@PathVariable Long houseId,@PathVariable Long id){
        //删除七牛云上照片
        HouseImage houseImage = houseImageService.getById(id);
        QiniuUtils.deleteFileFromQiniu(houseImage.getImageName());
        //删除数据库中照片记录
        houseImageService.delete(id);

        return "redirect:/house/" + houseId;
    }

}
